﻿using Chromely;
using Chromely.CefGlue.Browser.EventParams;
using Chromely.Core;
using Chromely.Core.Configuration;
using Chromely.Core.Host;
using Chromely.Core.Logging;
using System;
using Reader.Core.Application;
using System.Reflection;
using Reader.Controllers.Message;
using Reader.Core;
using Xilium.CefGlue.Wrapper;

namespace Reader
{
    public class ReaderApp : ChromelyEventedApp
    {
        public override void Configure(IChromelyContainer container)
        {
            base.Configure(container);
            container.RegisterSingleton(typeof(Applications), Guid.NewGuid().ToString(), typeof(Applications));

            container.RegisterSingleton(typeof(ProcessController), Guid.NewGuid().ToString(), typeof(WindowCommandsController));
            container.RegisterSingleton(typeof(ProcessController), Guid.NewGuid().ToString(), typeof(ExtensionController));
            container.RegisterSingleton(typeof(ProcessController), Guid.NewGuid().ToString(), typeof(BookController));
        }

        public override void RegisterEvents(IChromelyContainer container)
        {
            base.RegisterEvents(container);
        }

        public override void Initialize(IChromelyContainer container, IChromelyAppSettings appSettings, IChromelyConfiguration config, IChromelyLogger chromelyLogger)
        {
            base.Initialize(container, appSettings, config, chromelyLogger);

            //var appResource = container.GetInstance<ChromeReaderResourceHandlerFactory>("app-resource");
            //CefRuntime.RegisterSchemeHandlerFactory("app", "reader.com", appResource);
        }
        public override IChromelyWindow CreateWindow()
        {
            return base.CreateWindow();
        }
        protected override void OnBeforeClose(object sender, BeforeCloseEventArgs eventArgs)
        {
            base.OnBeforeClose(sender, eventArgs);
        }
        protected override void OnConsoleMessage(object sender, ConsoleMessageEventArgs eventArgs)
        {
            base.OnConsoleMessage(sender, eventArgs);
        }
        protected override void OnStatusMessage(object sender, StatusMessageEventArgs eventArgs)
        {
            base.OnStatusMessage(sender, eventArgs);
        }
        protected override void OnFrameLoadStart(object sender, FrameLoadStartEventArgs eventArgs)
        {
            //Configuration.JavaScriptExecutor.ExecuteScript($"console.log('{Configuration.StartUrl}')");
            
            string path = string.Empty;
#if DEBUG
            if (eventArgs.Frame.IsMain)
            {

                path = "init.js";
            }
            else
            {
                path = "init_frame.js";
            }
#else
            if (eventArgs.Frame.IsMain)
            {
                
                path = "init.min.js";
            }
            else
            {
                path = "init_frame.min.js";
            }
#endif
            var scripts = Utility.GetEmbedStringSourceAsync(path).Result;
            var url = eventArgs.Frame.Url;
            eventArgs.Frame.ExecuteJavaScript(scripts, url, 0);
            //Configuration.JavaScriptExecutor.ExecuteScript(scripts);
            base.OnFrameLoadStart(sender, eventArgs);
        }
        protected override void OnFrameLoadEnd(object sender, FrameLoadEndEventArgs eventArgs)
        {
            //var localStartFile = Configuration.StartUrl.Split(new char[] { '/' }).LastOrDefault();
            //var localStartPath = Configuration.StartUrl.Replace(localStartFile, "");
            //var obj = Configuration.UrlSchemes
            //    .Where(s => s.UrlSchemeType == UrlSchemeType.Resource)
            //    .Select(s => new
            //    {
            //        Protocol = $"{s.Scheme}:",
            //        s.Host,
            //        s.BaseUrl,
            //        StartPath = localStartPath
            //    });
            //var scheme = $"window.scheme = {JsonSerializer.Serialize(obj)}";
            //if (obj.Count() == 1)
            //{
            //    scheme = $"window.scheme = {JsonSerializer.Serialize(obj.Single())}";
            //}
            //var script = Configuration.JavaScriptExecutor;
            //script.ExecuteScript(scheme);
            //base.OnFrameLoadEnd(sender, eventArgs);
        }
    }
}
